jaccard <- function(M, user1, user2) { sums = rowSums(M[,c(user1, user2)]) similarity = length(sums[sums==2]) total = length(sums[sums==1]) + similarity similarity/total }